/**
 * 自定义函数
 * */


//列表
function getIndex(){
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'yw-mall/goods/list',
        data: {},
        success: function (res) {
            console.log(res);
            var h = '', tbody = $('tbody');
            if (res.code != 0) {
                h = '<tr class="text-c">';
                h += '<td colspan="10">没有记录</td>';
                h += '</tr>';
                tbody.html(h);
                return
            }
            formList = res.data;
            tbody.html(getHtml(res.data));
            $('#pageTotal').html(res.total);
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
//列表字符串
function getHtml(d){
    var h = '', i;
    h += '';
    for (i = 0; i < d.length; i ++) {
        h += '<tr class="text-c">';
        h += '<td>' + d[i].id + '</td>';
        h += '<td>' + d[i].title + '</td>';
        if (d[i].cover == '') {
            h += '<td>-</td>';
        } else {
            h += '<td><img src="/' + d[i].cover + '" width="100"></td>';
        }
        h += '<td>' + d[i].price + '</td>';
        h += '<td>' + d[i].num + '</td>';
        h += '<td class="td-manage">';
        h += '<a href="javascript:;" class="ml-5" onclick="lay_edit(\'编辑\',\'goods-edit.html\', ' + d[i].id + ');">';
        h += '编辑';
        h += '<i class="Hui-iconfont">&#xe6df;</i>';
        h += '</a>';
        h += '&nbsp;&nbsp;';
        h += '<a href="javascript:;" class="ml-5" onclick="lay_del(' + d[i].id + ');" style="color: red;">';
        h += ' 删除';
        h += '<i class="Hui-iconfont"></i>';
        h += '</a>';
        h += '<input type="hidden" id="title' + d[i].id +'" value="' + d[i].title + '"/>';
        h += '<input type="hidden" id="cover' + d[i].id +'" value="' + d[i].cover + '"/>';
        h += '<input type="hidden" id="price' + d[i].id +'" value="' + d[i].price + '"/>';
        h += '<input type="hidden" id="num' + d[i].id +'" value="' + d[i].num + '"/>';
        h += '</td>';
        h += '</tr>';
    }
    return h;
}


//编辑弹窗
function lay_edit(title,url,id,w,h){
    url += '?id=' + id;
    var tt = $('#title' + id).val();
    var cover = $('#cover' + id).val();
    var price = $('#price' + id).val();
    var num = $('#num' + id).val();
    tt = tt ? escape(tt) : '';
    cover = cover ? cover : '';
    price = price ? price : '';
    num = num ? num : '';
    url += '&title=' + tt;
    url += '&img=' + escape(cover);
    url += '&price=' + price;
    url += '&num=' + num;
    layer_show(title,url,w,h);
}


//获取详情
function getOne(){
    if (!id) { return }
    $('#title')[0].value = title;
    $('#price')[0].value = price;
    $('#num')[0].value = num;
    var h = '';
    if (img != '') {
        h = '<img src="/' + img + '"/>';
        h += '<input type="hidden" id="img" value="' + img + '"/>';
        $('#bigShow').html(h);
    }
}


//显示图片
function showImg(e){
    var h = '';
    h += '<img src="' + e.img + '" style="width: 150px;"/>';
    h += '<input type="hidden" id="img" value="' + e.img + '"/>';
    $('#bigShow').html(h);
}


//修改
function modify(){
    var formData = {
        id: id,
        title: $('#title').val(),
        img: $('#img').val(),
        price: $('#price').val(),
        num: $('#num').val(),
    };
    if (formData.title == '') {
        msgPop({msg: '名称必须的'}); return
    }
    console.log(formData);
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'yw-mall/goods/modify',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                //msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '操作成功'});
            //window.location.href = '';
            setTimeout(function(){
                top.location.reload();
            }, 500);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//删除
function lay_del(id) {
    var msg = '确认要 <b style="color: red; font-size: 20px;">删除</b> 吗？';
    layer.confirm(msg, function (index) {
        $.ajax({
            type: apiMethod,
            url: apiDomain + 'yw-mall/goods/del',
            data: {  id: id },
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    msgPop({msg: res.msg});
                    return
                }
                layer.msg('已删除!', {icon: 1, time: 1000});
                setTimeout(function () {
                    top.location.reload();
                }, 500);
            },
            error: function () {
                console.log('错误了！');
            }
        });
    });
}